<?php
namespace app\api\controller\user;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\buss\Buss;
use app\common\model\power\Rolepowerlnk;
use app\api\controller\Base;
use app\common\model\power\Rolepower;
use app\common\model\pub\Telbind;
class Manabuss extends Base
{
	/**
	 * 产生jwt加密串，参考session
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(201101);
	}

	public function getinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = Buss::user_getinfo($id);
		$data['userinfo'] = UserModel::getinfo('',$id);
		if($data['userinfo']['State']!==UserModel::STATEINT){
			$data['State'] = $value['userinfo']['State'] + 100;  
		}
		
		$data['StateText'] = Buss::getStatusText();
		$data['power'] = Rolepowerlnk::getpowerlnk('buss');
		//呼叫回访中心
		$data['call_center_switch'] = get_config_value('call_center_switch');
		if($data['call_center_switch']){
			//如果系统开启，则查询是否已经开启呼叫功能
			$tel = Telbind::get($id);
			if(empty($tel)){
				$data['TelBind'] = false;
			}else{
				$data['TelBind'] = true;
			}
		}
		return myok($data);
	}

	/**
	 * 查看管理列表
	 * @return [type] [description]
	 */
	public function list(){
		$data = input('post.');
		$data = init_where($data);
		$list = Buss::mgergetlist($data);
		foreach ($list['list'] as $key => $value) {
			$list['list'][$key]['UserState'] = $value['userinfo']['State'];
			if(empty($value['Address'])){
				$list['list'][$key]['Address'] = '未分配';
			}
			if($value['userinfo']['State']!==UserModel::STATEINT){
				$list['list'][$key]['State'] = $value['userinfo']['State'] + 100;  
			}
		}
		$list['tr']['title']  = ['收集员信息','所属区域','状态','查看信息'];
		$list['tr']['key'] = ['UserName','Address','State','UserId'];
		return myok($list);
	}

	/**
	 * 修改用户信息
	 * @return [type] [description]
	 */
	public function reinfo(){
		Rolepower::powerguard(204001);
		$data = input('get.');
		if(empty($data['id'])){
			return myerr('缺少核心参数');
		}
		if(!empty($data['State'])){
			if($data['State']!=='2'&&$data['State']!=='3'){
				return myerr('参数错误，请重新尝试');
			}
		}
		$sb = Buss::mana_reinfo($data);
		if($sb['code']){
			return myok('修改成功');
		}else{
			return myerr('修改失败');
		}
	}

	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
